package edu.zzuli.model.core.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import edu.zzuli.common.MisException;
import edu.zzuli.model.core.iface.PublicFacade;
import edu.zzuli.model.dao.BaseDaoEntity;
import edu.zzuli.model.dao.BasePO;

/**
 * @author tianshaojie
 * @date 2011-8-19
 * @discription :
 */
public class PublicFacadeImpl implements PublicFacade {
	
	private BaseDaoEntity baseDaoEntity;

	public BaseDaoEntity getBaseDaoEntity() {
		return baseDaoEntity;
	}
	public void setBaseDaoEntity(BaseDaoEntity baseDaoEntity) {
		this.baseDaoEntity = baseDaoEntity;
	}
	public String queryForString(String sql, Map mapPara) {
		return this.baseDaoEntity.queryForString(sql, mapPara);
	}
	public String queryForString(String sql, MapSqlParameterSource mapPara) {
		return this.baseDaoEntity.queryForString(sql, mapPara);
	}

	@Deprecated
	public List queryForList(String sql) {
		return this.baseDaoEntity.queryForList(sql);
	}

	public List queryForList(String sql, Map paraMap) {
		return this.baseDaoEntity.queryForList(sql, paraMap);
	}

	public Map queryForMap(String sql, Map mapPara) {
		return this.baseDaoEntity.selectSingleMap(sql, mapPara);
	}

	public BasePO selectSingle(BasePO basePO) {
		return this.baseDaoEntity.selectSingle(basePO);
	}

	public List selectExact(BasePO basePO, StringBuffer sbWhere) {
		return this.baseDaoEntity.getHibernateDao().selectExact(basePO, sbWhere);
	}

	public List queryForList(String sql, SqlParameterSource paramSource) {
		try {
			if (StringUtils.isEmpty(sql)) {
				throw new MisException("sql语句为空！");
			}
			if (paramSource == null) {
				throw new MisException("查询参数为null！");
			}
			return this.baseDaoEntity.getParaJdbcTemplate().queryForList(sql,
					paramSource);
		} catch (MisException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new MisException("[BaseDaoEntity.queryForList]异常！");
		}
	}

	/**
	 * 只能单独使用，不允许在facade方法中调用
	 * 
	 * @param sql
	 */
	public void execute(String sql) {
		try {
			if (StringUtils.isEmpty(sql)) {
				throw new MisException("sql语句为空！");
			}
			this.baseDaoEntity.getJdbcTemplate().execute(sql);
		} catch (MisException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new MisException("[BaseDaoEntity.queryForList]异常！");
		}
	}

	/**
	 * 只能单独使用，不允许在facade方法中调用
	 * 
	 * @param sql
	 * @param paramSource
	 */
	public void execute(String sql, SqlParameterSource paramSource) {
		try {
			if (StringUtils.isEmpty(sql)) {
				throw new MisException("sql语句为空！");
			}
			if (paramSource == null) {
				throw new MisException("查询参数为null！");
			}
			this.baseDaoEntity.getParaJdbcTemplate().update(sql, paramSource);
		} catch (MisException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new MisException("[BaseDaoEntity.queryForList]异常！");
		}
	}

	/**
	 * 只能单独使用，不允许在facade方法中调用
	 * 
	 * @param sql
	 * @param mapParam
	 */
	public void execute(String sql, Map mapParam) {
		try {
			if (StringUtils.isEmpty(sql)) {
				throw new MisException("sql语句为空！");
			}
			if (mapParam == null) {
				throw new MisException("查询参数为null！");
			}
			this.baseDaoEntity.getParaJdbcTemplate().update(sql, mapParam);
		} catch (MisException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new MisException("[BaseDaoEntity.queryForList]异常！");
		}
	}

	public void saveSingleBasePO(BasePO basePO) {
		try {
			if (basePO == null) {
				throw new MisException("参数为null！");
			}
			this.baseDaoEntity.getHibernateDao().save(basePO);

		} catch (MisException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new MisException("[saveSingleBasePO]异常！");
		}
	}

	

}
